<template>
  <el-dialog :visible.sync="visible" v-if="visible" width="50%" :title="formData.uniId ? '编辑大学' : '大学新增'" top="20px" :close-on-click-modal="false" >
    <el-form
      ref="ruleForm"
      :model="formData"
      :inline="false"
      :rules="rules"
      label-width="0"
      class="demo-ruleForm"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <p class="form_required">大学名称</p>
          <el-form-item prop="uniName">
            <el-input
              v-model="formData.uniName"
              placeholder="请输入大学名称"
              size="medium"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>城市</p>
          <el-form-item >
            <el-cascader
              size="large"
              :options="regionData"
              :props="{ children: 'child' }"
              v-model="selectedOptions"
            >
            </el-cascader>
            <el-input v-show="false" v-model="formData.cityCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>招生类型</p>
          <el-form-item >
            <el-select
              clearable
              class="filter-item"
              v-model="formData.enrollmentType"
              placeholder="请选择招生类型"
            >
              <el-option
                v-for="(item, index) in examTypes"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>层次</p>
          <el-form-item >
            <el-select
              clearable
              class="filter-item"
              v-model="formData.eduType"
              placeholder="请选择层次"
            >
              <el-option
                v-for="(item, index) in eduTypes"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        
        <!-- <el-col :span="12">
          <p>层次</p>
          <el-form-item >
            <el-select
              type="tags"
              clearable
              class="filter-item"
              v-model="formData.eduType"
              placeholder="请选择层次"
            >
              <el-option
                v-for="(item, index) in eduTypes"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <p>属性</p>
          <el-form-item >
            <el-select
              type="tags"
              multiple
              clearable
              class="filter-item"
              :remote="true"
              v-model="formData.tagList"
              filterable
              placeholder="请选择属性"
            >
              <el-option
                v-for="(item, index) in tagList"
                :key="index"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="!formData.uniId">
          <p>院校标识</p>
          <el-form-item >
            <el-select
              clearable
              class="filter-item"
              multiple
              v-model="formData.uniTypes"
              placeholder="请选择院校标识"
            >
              <el-option
                v-for="(item, index) in enrollmentTypes"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="!formData.uniId && showYear">
          <p>年份</p>
          <el-form-item >
            <el-select
              clearable
              class="filter-item"
              v-model="year"
              placeholder="请选择年份"
            >
              <el-option
                v-for="(item, index) in years"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        
        <el-col :span="12">
          <p>师资力量</p>
          <el-form-item >
            <el-input
            
              type="textarea"
              :rows="4"
              v-model="formData.teacherResource"
              placeholder="请输入师资力量"
              size="medium"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>学科实力</p>
          <el-form-item >
            <el-input
            
              type="textarea"
              :rows="4"
              v-model="formData.subjectLevel"
              placeholder="请输入学科实力"
              size="medium"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>学位授权点</p>
          <el-form-item >
            <el-input
            
              type="textarea"
              :rows="4"
              v-model="formData.degreePoints"
              placeholder="请输入学位授权点"
              size="medium"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>特殊规定</p>
          <el-form-item >
            <el-input
            
              type="textarea"
              :rows="4"
              v-model="formData.specialRequirements"
              placeholder="请输入特殊规定"
              size="medium"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>录取原则</p>
          <el-form-item >
            <el-input
            
              type="textarea"
              :rows="4"
              v-model="formData.admissionPrinciple"
              placeholder="请输入录取原则"
              size="medium"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>录取分数线说明</p>
          <el-form-item >
            <el-input
            
              type="textarea"
              :rows="4"
              v-model="formData.admissionLineDsc"
              placeholder="请输入录取分数线说明"
              size="medium"
              clearable
            />
          </el-form-item>
        </el-col>
        
        <el-col :span="12">
          <p>支持学位</p>
          <el-form-item >
            <el-select
              type="tags"
              multiple
              clearable
              class="filter-item"
              :remote="true"
              v-model="formData.degreeNameList"
              default-first-option
              filterable
              allow-create
              placeholder="请选择支持学位"
            >
              <el-option
                v-for="(item, index) in degreeNameList"
                :key="index"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>特殊算分公式</p>
          <el-form-item >
            <el-select
              class="filter-item"
              v-model="formData.specialFormula"
              placeholder="请选择支持学位"
            >
              <el-option
                v-for="(item, index) in specialList"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <p>背景图</p>
          <el-form-item >
            <el-upload
              v-loading="bgImgUrl_loading"
              class="avatar-uploader el-upload"
              action="https://jsonplaceholder.typicode.com/posts/"
              :http-request="(params) => uploadRequest(params, 'bgImgUrl')"
              :show-file-list="false"
            >
              <img
                v-if="formData.bgImgUrl"
                :src="formData.bgImgUrl"
                class="avatar"
              />
              <div v-else class="upload-text">选择图片</div>
            </el-upload>
            <el-input v-show="false" v-model="formData.districtCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <p>校徽</p>
          <el-form-item >
            <el-upload
              v-loading="emblem_loading"
              class="avatar-uploader el-upload"
              action="https://jsonplaceholder.typicode.com/posts/"
              :http-request="(params) => uploadRequest(params, 'emblem')"
              :show-file-list="false"
            >
              <img
                v-if="formData.emblem"
                :src="formData.emblem"
                class="avatar"
              />
              <div v-else class="upload-text">选择图片</div>
            </el-upload>
            <el-input v-show="false" v-model="formData.emblem"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <!-- <el-col :span="12">
          <p>地址</p>
          <el-form-item >
            <el-input
              type="textarea"
              :rows="4"
              v-model="formData.address"
              placeholder="请输入详细地址"
              size="medium"
              clearable
            />
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <p>简介</p>
          <el-form-item >
            <el-input
              type="textarea"
              :rows="8"
              v-model="formData.intro"
              placeholder="请输入简介"
              size="medium"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <p>师资力量</p>
  <el-form-item prop="describeInfo">
    <el-input v-model="formData.describeInfo" placeholder="请输入描述" size="medium" clearable />
  </el-form-item>
  <p>教师照片</p>
  <el-form-item>
    <el-image fit="cover" lazy :preview-src-list="teacherPhotos" class="t_img" v-for="(item,index) in teacherPhotos" :key="index" :src="item"></el-image>
    <el-upload
      v-loading="img_loading"
      class="avatar-uploader el-upload"
      action="https://jsonplaceholder.typicode.com/posts/"
      :http-request="(params)=>uploadRequest(params,'teacher')"
      :show-file-list="false"
    >
      <div class="upload-text">选择封面图片</div>
    </el-upload>
  </el-form-item> -->
      <!-- <p>内容</p>
  <el-form-item prop="content">
    <tinymce ref="ty" v-model="formData.content" :height="300" />
  </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="confirm(true)" :loading="submitLoading"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import Tinymce from "@/components/Tinymce";
import { uploadFile } from "@/api/admin.js";
import { getInfoById, add, getTags } from "@/api/web/school";
import regionData from "@/utils/area";
import {getYears} from '@/utils/gaobao'
export default {
  components: { Tinymce },
  data() {
    return {
      regionData,
      submitLoading: false,
      loading: false,
      formData: {
        specialFormula:1,
      },
      bgImgUrl_loading: false,
      emblem_loading: false,
      teacherPhotos: [],
      selectedOptions: [],
      visible: false,
      years:getYears(),
      tagList: [],
      uniTypeList:[],
      degreeNameList:[],
      year:'',
      enrollmentTypes: [
        { label: '停招院校', value: 1 },
        { label: '新增院校', value: 2 },
        { label: '特殊院校', value: 3 },
        { label: '校考院校', value: 4 },

      ],
      examTypes: [
                { label: '统考', value: 1 },
                { label: '校考', value: 2 },

            ],
            specialList: [
                { label: '有', value: true },
                { label: '无', value: false },

            ],
            eduTypes:[
              
            { label: '专科', value: 1 },
                { label: '本科', value: 2 },
            ],
      rules: {
        address: [
          {
            required: true,
            message: "请填写学校地址",
          },
        ],
        districtCode: [
          {
            required: true,
            message: "请选择学校地区",
          },
        ],
        bgImgUrl: [
          {
            required: true,
            message: "请上传大学背景图",
          },
        ],
        intro: [
          {
            required: true,
            message: "介绍一下大学吧~",
          },
        ],
        uniName: [
          {
            required: true,
            message: "大学名称不能为空",
          },
        ],
      },
    };
  },
  mounted() {
    console.log("this.regionData :>> ", this.regionData);
    if (this.$route.query.id) {
      this.getInfoById();
    }
  },
  watch: {
    selectedOptions(newValue, oldValue) {
      console.log("newValue :>> ", newValue);
      if (newValue.length) {
        this.$set(this.formData, "cityCode", newValue[newValue.length - 1]);
      } else {
        this.$set(this.formData, "cityCode", "");
      }
    },
  },
  computed: {
    showYear() {
      return this.formData.uniTypes.includes(1) || this.formData.uniTypes.includes(2)
    }
  },
  methods: {
    add(query) {
      this.formData = {}
      this.selectedOptions = []
      this.$set(query,'uniTypes',[])
      this.edit(query);
    },
    edit(record) {
      this.visible = true;
      if (record.selectedOptions && record.selectedOptions.length) {
        this.selectedOptions = record.selectedOptions
      }
      this.formData = JSON.parse(JSON.stringify(record));
      getTags().then((res) => {
        if (res.code == 200) {
          this.tagList = res.data.tagList;
        }
      });
      if (record.uniId) {
        this.getInfoById();
      }
    },
    getAuthor(e) {
      getAuthor({ page: 1, limit: 10, name: e }).then((res) => {
        if (res.code == 200) {
          this.authorList = res.data.list;
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      });
    },
    getInfoById() {
      this.loading = true;
      getInfoById({ uniId: this.formData.uniId }).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          this.formData = res.data;
          this.selectedOptions = [
            this.formData.provinceCode,
            this.formData.cityCode,
          ];
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      });
    },
    uploadRequest(params, key) {
      this[`${key}_loading`] = true;
      uploadFile(params).then((res) => {
        this[`${key}_loading`] = false;
        if (res && res.data && res.data.length) {
          if (key === "teacher") {
            this.teacherPhotos.push(res.data[0]);
          } else {
            this.$set(this.formData, key, res.data[0]);
          }
        }
      });
    },
    /**
     * 保存
     */
    confirm(closeDialog) {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          let saveForm = JSON.parse(JSON.stringify(this.formData))
          if (saveForm.uniTypes && saveForm.uniTypes.length) {
            saveForm.uniTypeList = saveForm.uniTypes.map(v=>{
              let obj ={uniType:v}
              if (v == 1 || v == 2) {
                obj.year = saveForm.year
              }
              return obj
            })
          
          }
          if (!saveForm.degreeNameList) {
            saveForm.degreeNameList = []
          }
          if (!saveForm.cityCode) {
            delete saveForm.cityCode
          }
          add(saveForm).then((res) => {
            this.submitLoading = false;
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "提交成功",
              });
              this.$emit("ok");
              this.visible = false;
            } else {
              this.$message({
                type: "error",
                message: res.message,
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
  
  <style scoped>
.avatar-uploader,
.el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
}
.avatar-uploader:hover,
.el-upload:hover {
  border-color: #409eff;
}
.upload-text {
  font-size: 14px;
  color: #8c939d;
  line-height: 178px;
  text-align: center;
  margin: 0 auto;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.container {
  padding-bottom: 30px;
}
.t_img {
  display: inline-block;
  width: 178px;
  height: 178px;
  margin-right: 12px;
  margin-top: 12px;
}
.form_required::before {
  content: "*";
  color: red;
}
</style>
  <style>
.demo-ruleForm .el-input,
.demo-ruleForm .el-select {
  width: 300px;
}
.el-dialog__header {
  border-bottom: 1px solid #e8e8e8;
}
.el-dialog__footer {
  padding: 20px !important;
  border-top: 1px solid #e8e8e8;
}
</style>
  